package com.sptpc.demo.controller;

import com.sptpc.demo.global.exception.BusinessException;
import com.sptpc.demo.global.response.BusinessesStatusCode;
import com.sptpc.demo.global.response.GlobalResponse;
import com.sptpc.demo.model.Drink;
import com.sptpc.demo.service.IDrinkService;
import com.sptpc.demo.vo.DrinkVo;
import com.sptpc.demo.vo.DrinkWithCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/drink")
public class DrinkController {
    @Autowired
    private IDrinkService drinkService;

    // get /drink/
    //@RequestMapping(value = "/", method = RequestMethod.GET)
    @GetMapping("/")
    public List<DrinkVo> getAll() {
        // mock数据 - 后期从数据库查询
        return drinkService.getAllDrinks();
    }
    @GetMapping("/drinks")
    public List<DrinkWithCategoryVo> getDrinkWithCategory() {
        // mock数据 - 后期从数据库查询
        return drinkService.getdrinkWithCategory();
    }

    @GetMapping("/{id}")
    public GlobalResponse getDrink(@PathVariable Long id) {
        log.info("要查询的饮品",id);
        Drink drink=drinkService.getDrinkById(id);
        if (drink==null){
            throw  new BusinessException(
                    BusinessesStatusCode.DRINK_NOT_FOUND
            );
        }
        // mock数据 - 后期从数据库查询
        return GlobalResponse.success(drink);
    }
//
//    /**
//     * 查询指定饮品
//     * get /drink/1
//     * get /drink/?id=1
//     * 日志 log4j
//     */
//    @GetMapping("/{id}")
//    public Drink getDrink(@PathVariable Integer id) {
//        log.info("要查询的饮品id: {}", id);
//        // 后期从数据库查询
//        Drink drink = Drink.builder().id(1)
//                .name("茉莉花茶")
//                .price(Money.parse("USD 8.00"))
//                .build();
//        return drink;
//    }
//
    /**
     * 新建
     * pos  t /drink/ {"name": "奶茶", "price": "8.00", "categoryId": "2"}
     * 传参 思路:
     * 1. 直接使用实体类
     * 2. 根据前端参数内容编写TO类
     */
    @PostMapping("/")
    public Drink addNewDrink(@RequestBody @Valid Drink drink,
                             BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                log.error(error.toString());
            }
            return null;
        } else {
            log.info("新增饮品：{}", drink);
            drinkService.addNewDrink(drink);
            return  drink;
        }
    }

//    /**
//     * 修改
//     * put /drink/ {"id": "1", “name”: "新名称", "price": "9.00"}
//     */
    @PutMapping("/")
    public Drink modifyDrink(@RequestBody Drink request) {
        log.info("要修改的饮品：{}", request);
        //drink JPA server更新方法
        // 1、通过id查询出饮品原来值
        //Drink drink = drinkService.getDrinkById(request.getId());
        // 2、复制前端要修改的属性
        //BeanUtils.copyProperties(request, drink);
        // 3、更新数据库
        //   drinkService.updateDrink(drink);

        /**
         *
         */
        drinkService.updateDrink(request);

        return request;
    }
//
//    /**
//     * 删除
//     * delete /drink/1
//     */
    @DeleteMapping("/{id}")
    public String deleteDrink(@PathVariable Long id) {
        log.info("要删除的饮品id：{}", id);
        drinkService.deleteDrinkById(id);
        return "删除操作成功";
    }

}
